<?php
/**
* 商品服务
*/
class ElemeProductSvc extends ElemeCommon
{
    
    /** 添加商品
     * @param $category_id 商品分类Id
     * @param $name 商品名称
     * @param $description 商品描述
     * @param $imageHash 商品图片
     * @param $specsInfo 商品规格信息
     */
    public function createItem($category_id = '', $name = '', $description = '', $imageHash = '', $specsInfo = array())
    {
        $data = array();
        $data['categoryId'] = $category_id;

        $data['properties'] = array();
        $data['properties']['name'] = $name;
        $data['properties']['description'] = $description;
        $data['properties']['imageHash'] = $imageHash;

        $data['properties']['specs'] = array($specsInfo);
        
        return $data;
    }

    /** 获取商品规格
     * @param $specId 规格Id
     * @param $specName 规格名称
     * @param $price 商品价格
     * @param $stock 库存量
     * @param $maxStock 最大库存量
     * @param $packingFee 包装费
     * @param $onShelf 是否上架
     * @param $extendCode 商品扩展码
     * @param $barCode 商品条形码
     * @param $weight 商品重量
     * @param $activityLevel 商品活动信息，1:有活动，0:无活动，
     */
    public function getItemSpecs($specId = '', $specName = '', $price = '', $stock = 0, $maxStock = '', $packingFee = '', $onShelf = 1, $extendCode = '', $barCode = '', $weight = '', $activityLevel = 0)
    {
        $specs = array();

        $specs['specId'] = $specId;
        $specs['name'] = $specName;
        $specs['price'] = $price;
        $specs['stock'] = 0;
        $specs['maxStock'] = 0;
        $specs['packingFee'] = $packingFee;
        $specs['onShelf'] = $onShelf;
        $specs['extendCode'] = $extendCode;
        $specs['barCode'] = $barCode;
        $specs['weight'] = $weight;
        $specs['activityLevel'] = $activityLevel;        
                
        return $specs;
    }

    /** 
     *   发送添加商品
     */
    public function sendCreateItem($data = array())
    {
        $action = 'eleme.product.item.createItem';
        $postData = $this->getPostData($action, $data);

        $this->DLOG("create_product : " . json_encode($postData), "run", "channel_eleme");

        $res = $this->doPost($this->api_request_url, $postData);

        $this->DLOG("create_product_res : " . json_encode($res, JSON_UNESCAPED_UNICODE), "run", "channel_eleme");

        return $res;
    }

    /** 批量上架商品
     * @param $itemId 商品Id
     * @param $itemSpecIds 商品规格Id的列表
     */
    public function batchOnShelf($itemId = '', $itemSpecIds = '')
    {
        $spec_ids_object = array();

        $spec_ids_object["itemId"] = $itemId;
        $item_spec_ids = array();
        $item_spec_ids_object = $itemSpecIds;
        $item_spec_ids[] = $item_spec_ids_object;
        $spec_ids_object["itemSpecIds"] = $item_spec_ids;

        return $spec_ids_object;
    }

    /** 
        批量上架商品拼数据
     */
    public function batchOnShelfData($spec_ids_object = '')
    {
        $spec_ids = array();

        $spec_ids['specIds'] = $spec_ids_object;

        return $spec_ids;
    }

    /** 
     *   发送批量上架商品
     */
    public function sendBatchOnShelf($data = array())
    {
        $action = 'eleme.product.item.batchOnShelf';
        $postData = $this->getPostData($action, $data);

        $this->DLOG("batch_on_shelf : " . json_encode($postData), "run", "channel_eleme");

        $res = $this->doPost($this->api_request_url, $postData);

        $this->DLOG("batch_on_shelf_res : " . json_encode($res, JSON_UNESCAPED_UNICODE), "run", "channel_eleme");

        return $res;
    }

    /** 批量下架商品
     * @param $itemId 商品Id
     * @param $itemSpecIds 商品规格Id的列表
     */
    public function batchOffShelf($itemId = '', $itemSpecIds = '')
    {
        $spec_ids_object = array();

        $spec_ids_object["itemId"] = $itemId;
        $item_spec_ids = array();
        $item_spec_ids_object = $itemSpecIds;
        $item_spec_ids[] = $item_spec_ids_object;
        $spec_ids_object["itemSpecIds"] = $item_spec_ids;

        return $spec_ids_object;
    }

    /** 
        批量下架商品拼数据
     */
    public function batchOffShelfData($spec_ids_object = '')
    {
        $spec_ids = array();

        $spec_ids['specIds'] = $spec_ids_object;

        return $spec_ids;
    }

    /** 
     *   发送批量下架商品
     */
    public function sendBatchOffShelf($data = array())
    {
        $action = 'eleme.product.item.batchOffShelf';
        $postData = $this->getPostData($action, $data);

        $this->DLOG("batch_off_shelf : " . json_encode($postData), "run", "channel_eleme");

        $res = $this->doPost($this->api_request_url, $postData);

        $this->DLOG("batch_off_shelf_res : " . json_encode($res, JSON_UNESCAPED_UNICODE), "run", "channel_eleme");

        return $res;
    }

    /** 修改商品
     * @param $itemId 商品Id
     * @param $categoryId 商品分类Id
     * @param $name 商品名称
     * @param $description 商品描述
     * @param $imageHash 图片imageHash
     * @param $specsInfo 商品规格信息
     */
    public function updateItem($itemId = '', $categoryId = '', $name = '', $description = '', $imageHash = '', $specsInfo = array())
    {
        $data = array();
        $data['itemId'] = $itemId;
        $data['categoryId'] = $categoryId;

        $data['properties'] = array();
        $data['properties']['name'] = $name;
        $data['properties']['description'] = $description;
        $data['properties']['imageHash'] = $imageHash;

        $data['properties']['specs'] = array($specsInfo);

        return $data;
    }

    /** 
     *   发送修改商品
     */
    public function sendUpdateItem($data = array())
    {
        $action = 'eleme.product.item.updateItem';
        $postData = $this->getPostData($action, $data);

        $this->DLOG("update_item : " . json_encode($postData), "run", "channel_eleme");

        $res = $this->doPost($this->api_request_url, $postData);

        $this->DLOG("update_item_res : " . json_encode($res, JSON_UNESCAPED_UNICODE), "run", "channel_eleme");

        return $res;
    }

    /** 获取一个分类下的所有商品
     * @param $categoryId 分类Id
     */
    public function getItemsByCategoryId($categoryId = '')
    {
        $data = array();
        $data['categoryId'] = $categoryId;      

        return $data;
    }

    /** 
     *   发送获取一个分类下的所有商品
     */
    public function sendGetItemsByCategoryId($data = array())
    {
        $action = 'eleme.product.item.getItemsByCategoryId';
        $postData = $this->getPostData($action, $data);

        $this->DLOG("product_item : " . json_encode($postData), "run", "channel_eleme");

        $res = $this->doPost($this->api_request_url, $postData);

        $this->DLOG("product_item_res : " . json_encode($res, JSON_UNESCAPED_UNICODE), "run", "channel_eleme");

        return $res;
    }

    /** 查询商品详情
     * @param $itemId 商品Id
     */
    public function getItem($itemId = '')
    {
        $data = array();
        $data['itemId'] = $itemId;      

        return $data;
    }

    /** 
     *   发送查询商品详情
     */
    public function sendGetItem($data = array())
    {
        $action = 'eleme.product.item.getItem';
        $postData = $this->getPostData($action, $data);

        $this->DLOG("get_item : " . json_encode($postData), "run", "channel_eleme");

        $res = $this->doPost($this->api_request_url, $postData);

        $this->DLOG("get_item_res : " . json_encode($res, JSON_UNESCAPED_UNICODE), "run", "channel_eleme");

        return $res;
    }

    /** 删除商品
     * @param $itemId 商品Id
     */
    public function removeItem($itemId = '')
    {
        $data = array();
        $data['itemId'] = $itemId;      

        return $data;
    }

    /** 
     *   发送删除商品
     */
    public function sendRemoveItem($data = array())
    {
        $action = 'eleme.product.item.removeItem';
        $postData = $this->getPostData($action, $data);

        $this->DLOG("remove_item : " . json_encode($postData), "run", "channel_eleme");

        $res = $this->doPost($this->api_request_url, $postData);

        $this->DLOG("remove_item_res : " . json_encode($res, JSON_UNESCAPED_UNICODE), "run", "channel_eleme");

        return $res;
    }

    /** 批量删除商品
     * @param $itemIds 商品Id的列表
     */
    public function batchRemoveItems($itemIds = '')
    {
        $data = array();
        $data['itemIds'] = $itemIds;

        return $data;
    }

    /** 
     *   发送批量删除商品
     */
    public function sendBatchRemoveItems($data = array())
    {
        $action = 'eleme.product.item.batchRemoveItems';
        $postData = $this->getPostData($action, $data);

        $this->DLOG("batch_remove_items : " . json_encode($postData), "run", "channel_eleme");

        $res = $this->doPost($this->api_request_url, $postData);

        $this->DLOG("batch_remove_items_res : " . json_encode($res, JSON_UNESCAPED_UNICODE), "run", "channel_eleme");

        return $res;
    }

    /** 通过远程URL上传图片，返回图片的hash值
     * @param $url 远程Url地址
     */
    public function uploadImageWithRemoteUrl($url = '')
    {
        $data = array();
        $data['url'] = $url;

        return $data;
    }

    /** 
     *   发送通过远程URL上传图片，返回图片的hash值
     */
    public function sendUploadImageWithRemoteUrl($data = array())
    {
        $action = 'eleme.file.uploadImageWithRemoteUrl';
        $postData = $this->getPostData($action, $data);

        $this->DLOG("upload_image_with_remote_url : " . json_encode($postData), "run", "channel_eleme");

        $res = $this->doPost($this->api_request_url, $postData);

        $this->DLOG("upload_image_with_remote_url_res : " . json_encode($res, JSON_UNESCAPED_UNICODE), "run", "channel_eleme");

        return $res['result'];
    }

    /** 上传图片，返回图片的hash值
     * @param $image 文件内容base64编码值
     */
    public function uploadImage($image = '')
    {
        $data = array();
        $data['image'] = $image;

        return $data;
    }

    /** 
     *   发送上传图片，返回图片的hash值
     */
    public function sendUploadImage($data = array())
    {
        $action = 'eleme.file.uploadImage';
        $postData = $this->getPostData($action, $data);

        $this->DLOG("upload_image : " . json_encode($postData), "run", "channel_eleme");

        $res = $this->doPost($this->api_request_url, $postData);

        $this->DLOG("upload_image_res : " . json_encode($res, JSON_UNESCAPED_UNICODE), "run", "channel_eleme");

        return $res;
    }

    /** 批量更新库存
     * @param $itemId   商品Id
     * @param $specId   商品规格Id
     * @param $stock    商品库存
     */
    public function batchUpdateSpecStocks($itemId = '', $specId = '', $stock = 0)
    {
        
        $spec_stocks_object = array();

        $spec_stocks_object["itemId"] = $itemId;

        $stock_map = array();

        $stock_map[$specId] = $stock;
        $spec_stocks_object["stockMap"] = $stock_map;

        return $spec_stocks_object;
    }

    /** 批量更新库存拼数据
     */
    public function batchUpdateSpecStocksData($spec_stocks_object = '')
    {
        $spec_stocks = array();

        $spec_stocks['specStocks'] = $spec_stocks_object;

        return $spec_stocks;
    }

    /** 
     *   发送批量更新库存
     */
    public function sendBatchUpdateSpecStocks($data = array())
    {
        $action = 'eleme.product.item.batchUpdateSpecStocks';
        $postData = $this->getPostData($action, $data);

        $this->DLOG("batch_update_spec_stocks : " . json_encode($postData), "run", "channel_eleme");

        $res = $this->doPost($this->api_request_url, $postData);

        $this->DLOG("batch_update_spec_res : " . json_encode($res, JSON_UNESCAPED_UNICODE), "run", "channel_eleme");

        return $res;
    }

    /** 批量更新价格
     * @param $itemId   商品Id
     * @param $specId   商品规格Id
     * @param $price    商品价格
     */
    public function batchUpdatePrices($itemId = '', $specId = '', $price = 0)
    {
        
        $spec_prices_object = array();

        $spec_prices_object["itemId"] = $itemId;

        $stock_map = array();

        $stock_map[$specId] = $price;
        $spec_prices_object["stockMap"] = $stock_map;

        return $spec_prices_object;
    }

    /** 批量更新价格拼数据
        other_shop_id  第三方店铺ID
        spec_prices_object 商品价格信息
     */
    public function batchUpdatePricesData($other_shop_id = 0, $spec_prices_object = '')
    {
        $product_prices = array();

        $product_prices['shopId'] = $other_shop_id;
        $product_prices['specPrices'] = $spec_prices_object;

        return $spec_prices;
    }

    /** 
     *   发送批量更新价格
     */
    public function sendBatchUpdatePricesData($data = array())
    {
        $action = 'eleme.product.item.batchUpdatePrices';
        $postData = $this->getPostData($action, $data);

        $this->DLOG("batch_update_prices : " . json_encode($postData), "run", "channel_eleme");

        $res = $this->doPost($this->api_request_url, $postData);

        $this->DLOG("batch_update_prices_res : " . json_encode($res, JSON_UNESCAPED_UNICODE), "run", "channel_eleme");

        return $res;
    }

    
}